//
//  TJSearchFriendsController.m
//  Teleport
//
//  Created by xtjun on 2020/6/13.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJSearchFriendsController.h"
 
 
@interface TJSearchFriendsController ()<UITextFieldDelegate>

@property (weak, nonatomic) UITextField *phoneTextField;
@property (weak, nonatomic) UIButton *areaButton;
@property (weak, nonatomic) UIButton *determineButton;


@end

@implementation TJSearchFriendsController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationBar.title = @"邀请好友".localizedString;
    self.navigationBar.lineView.hidden = YES;

    UIButton *areaButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(14) color:UIColor_Hex(0x262626) title:@"+86"];
    [areaButton setImage:[UIImage imageNamed:@"areaArrowIcon"] forState:UIControlStateNormal];
    [areaButton setImage:[UIImage imageNamed:@"areaArrowIcon"] forState:UIControlStateHighlighted];
    [areaButton setImagePosition:LXMImagePositionRight spacing:4];
    [areaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(36);
        make.top.offset(KNavBarHight + 50);
        make.width.offset(50);
        make.height.offset(24);
    }];
    
    UITextField *phoneTextField = [UITextField tj_addToView:self.view];
    phoneTextField.placeholder = @"请输入手机号码".localizedString;
    phoneTextField.font = UIFont_Px(15);
    [phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(areaButton.mas_right).offset(16);
        make.centerY.equalTo(areaButton);
        make.height.offset(34);
        make.right.offset(-36);
    }];
    
    UIView *lineView = [UIView tj_addToView:self.view];
    lineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(areaButton);
        make.right.equalTo(phoneTextField);
        make.height.offset(1);
        make.top.equalTo(phoneTextField.mas_bottom).offset(4);
    }];
    
    UIButton *determineButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(16) color:UIColor.whiteColor title:@"立即邀请".localizedString];
    determineButton.backgroundColor = UIColor.tj_mainColor;
    [determineButton shearRoundedCornersWithRadiu:3];
    [determineButton addTarget:self action:@selector(didClickDetermineButton) forControlEvents:UIControlEventTouchUpInside];
    [determineButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(56);
        make.right.offset(-56);
        make.height.offset(44);
        make.top.equalTo(lineView.mas_bottom).offset(50);
    }];
 
    _phoneTextField = phoneTextField;
    _determineButton = determineButton;
    [self textFieldDidChange:_phoneTextField];
}
 
- (void)textFieldDidChange:(UITextField *)textField {
    _determineButton.userInteractionEnabled = textField.text.replacingEmptyString.length;
    _determineButton.backgroundColor = (_determineButton.userInteractionEnabled) ? UIColor.tj_mainColor : [UIColor_Hex(0x666666) colorWithAlphaComponent:0.3];
}

- (void)didClickDetermineButton {
    
    NSString *phone = _phoneTextField.text;
    if (!phone.length) {
        [TJHud showAutoHideMessage:_phoneTextField.placeholder];
        return;
    }
    
    NSDictionary *parameters = @{
        @"token": [TJUserInfo.readInfo token],
        @"type": @"1",
        @"phone": _phoneTextField.text,
        @"areaCode": @"86",
    };
    
    [self showloading];
    
    [TJNetworking.shareNetworking POST:@"user/addFriend" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        [self hiddenloading];
        [self alertWithTitle:@"提示".localizedString message:responseObject[@"msg"] leftButtonName:@"知道了".localizedString rightButtonName:nil leftButtonBlock:^{
            [self.navigationController popViewControllerAnimated:YES];
        } rightButtonBlock:nil];
        
    } failure:^(NSError * _Nonnull error) {
        [self hiddenloading];
        [TJHud showAutoHideMessage:error.localizedFailureReason];
    }];
    
}


@end
